package com.weather.androidweather.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.weather.androidweather.R;
import com.weather.androidweather.model.DayForecast;
import java.util.ArrayList;
import java.util.List;

public class ForecastDetailAdapter extends RecyclerView.Adapter<ForecastDetailAdapter.ViewHolder> {
    private List<DayForecast> forecasts = new ArrayList<>();

    public void setForecasts(List<DayForecast> forecasts) {
        this.forecasts = forecasts;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_forecast_detail, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        DayForecast forecast = forecasts.get(position);
        
        // 设置日期
        String dateLabel;
        if (position == 0) {
            dateLabel = "今天";
        } else if (position == 1) {
            dateLabel = "明天";
        } else {
            dateLabel = forecast.getFormattedDate();
        }
        holder.dateText.setText(dateLabel);
        
        // 设置天气信息
        holder.weatherIcon.setImageResource(forecast.getWeatherIconRes());
        holder.weatherText.setText(forecast.getWeather());
        holder.temperatureText.setText(String.format("%.0f°/%.0f°", 
            forecast.getMaxTemp(), forecast.getMinTemp()));
        holder.windText.setText(forecast.getWindDir());
    }

    @Override
    public int getItemCount() {
        return forecasts.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView dateText;
        ImageView weatherIcon;
        TextView weatherText;
        TextView temperatureText;
        TextView windText;

        ViewHolder(View itemView) {
            super(itemView);
            dateText = itemView.findViewById(R.id.dateText);
            weatherIcon = itemView.findViewById(R.id.weatherIcon);
            weatherText = itemView.findViewById(R.id.weatherText);
            temperatureText = itemView.findViewById(R.id.temperatureText);
            windText = itemView.findViewById(R.id.windText);
        }
    }
} 